package org.example.zk;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * zkClient工厂类
 */
public class ClientFactory {

    /**
     * newClient创建
     * @param connectionStr
     * @return
     */
    public static CuratorFramework createSimple(String connectionStr) {
        // TODO 重试策略：第一次重试等待1s，第二次重试等待2s，第三次重试等待3s
        // TODO 第一个参数：等待时间的基础单位，单位为毫秒
        // TODO 第二个参数：最大重试次数
        ExponentialBackoffRetry retry = new ExponentialBackoffRetry(1000, 3);
        return CuratorFrameworkFactory.newClient(connectionStr, retry);
    }

    /**
     * build创建
     * @param connectionStr
     * @param retryPolicy
     * @param connectionTimeoutMs
     * @param sessionTimeoutMs
     * @return
     */
    public static CuratorFramework createWithOptions(String connectionStr, RetryPolicy retryPolicy, int connectionTimeoutMs, int sessionTimeoutMs) {
        return CuratorFrameworkFactory.builder().connectString(connectionStr)
                .connectionTimeoutMs(connectionTimeoutMs)
                .sessionTimeoutMs(sessionTimeoutMs)
                .retryPolicy(retryPolicy)
                .build();
    }
}
